using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace CKS.Sandbox.TipOfTheDay
{
    [Guid("772e4c5d-d6f3-464e-833b-77d281a836f0")]
    public class TipOfTheDayEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            string sampleTip = "<table width='35%'><tr> <td rowspan='2'> <img src='../../SiteAssets/TipOfTheDay/Idea.jpg'></td><td><b>Sandbox Solutions are cool</b></td></tr><tr><td> Now Farm administrators will be free from the tasks of controlling and managing all the requests for solution installs and be able to focus on the overall health of the farm with a set of rich monitoring tools.</td></tr></table>";

            SPWeb site = ((SPSite)properties.Feature.Parent).RootWeb;
            SPList instance = site.Lists.TryGetList("Tips");
            if (instance == null)
            {
                Guid id = site.Lists.Add("Tips", "A list which stores tip of the day.", SPListTemplateType.GenericList);
                instance = site.Lists[id];
                instance.Fields.Add("Text", SPFieldType.Note, false);
           }

             instance = site.Lists.TryGetList("TipStore");
            if (instance == null)
            {
                Guid id = site.Lists.Add("TipStore", "A list which stores selected tip of the day.", SPListTemplateType.GenericList);
                instance = site.Lists[id];
                instance.Hidden = true;
                instance.Fields.Add("Text", SPFieldType.Note, false);
                instance.Fields.Add("DateAdded", SPFieldType.DateTime, false);
                instance.Update();
                SPListItem tip = instance.Items.Add();
                tip.Fields["Text"].Type = SPFieldType.Note;
                tip.Fields["DateAdded"].Type = SPFieldType.DateTime;
                tip.Update();
                tip["Text"] = sampleTip;
                tip.Update();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb site = ((SPSite)properties.Feature.Parent).RootWeb;
            SPList instance = site.Lists.TryGetList("Tips");
            if (instance != null)
            {
                instance.Delete();
            }

            instance = site.Lists.TryGetList("TipStore");
            if (instance != null)
            {
                instance.Delete();
            }
        }
    }
}
